/*
 *  big_endian_tool.c
 *
 *  Copyright (C) 2011  Jason<linux8192@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include "debug.h"
#include "type.h"


u8 read_u8(const char *buf)
{
	u8	result;

	((char *)(& result))[7] = buf[0];
	((char *)(& result))[6] = buf[1];
	((char *)(& result))[5] = buf[2];
	((char *)(& result))[4] = buf[3];
	((char *)(& result))[3] = buf[4];
	((char *)(& result))[2] = buf[5];
	((char *)(& result))[1] = buf[6];
	((char *)(& result))[0] = buf[7];

	return result;
}

u4 read_u4(const char* buf)
{
	u4	result;

	((char *)(& result))[3] = buf[0];
	((char *)(& result))[2] = buf[1];
	((char *)(& result))[1] = buf[2];
	((char *)(& result))[0] = buf[3];

	return result;
}

inline u2 read_u2(const char *buf)
{
	u2	result;

	((char *)(& result))[1] = buf[0];
	((char *)(& result))[0] = buf[1];

	return result;
}

inline u1 read_u1(const char *buf)
{
	return buf[0];
}

u8 read_u8_and_move_cur(char **buf)
{
	u8 t = read_u8(*buf);
	*buf += 8;
	return t;
}

u4 read_u4_and_move_cur(char **buf)
{
	u4 t = read_u4(*buf);
	*buf += 4;
	return t;
}

u2 read_u2_and_move_cur(char **buf)
{
	u2 t = read_u2(*buf);
	*buf += 2;
	return t;
}

u1 read_u1_and_move_cur(char **buf)
{
	u1 t = read_u1(*buf);
	*buf += 1;
	return t;
}
